<template>
  <el-cascader
    :value="value"
    size="mini"
    :options="treeData"
    :props="props"
    separator="-"
    @change="changeValue"
  />
</template>

<script>
import { getDepartment } from '@/api/department'
import { getDepartmentListToTree } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      treeData: [], // 赋值给 级联组件的options
      props: {
        value: 'id', // 要存储的字段 控制存储/请求接口 (id: 接口文档中要求传递的部门id)
        label: 'name' // 要展示的字段
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    changeValue(list) {
      // console.log(list)
      if (list.length > 0) {
        //
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    },
    async getDepartment() {
      const res = await getDepartment()
      //   console.log(res)
      //   把得到的列表数据转成树形数据
      this.treeData = getDepartmentListToTree(res, 0)
    }
  }

}
</script>

<style>

</style>
